﻿namespace Dcms.UpdateModule
{
    using Dcms.Common;
    using System;
    using System.Collections;
    using System.Data;
    using System.Web;
    using System.Configuration;

    public class DcmsLinkProcessCode
    {
        private static readonly string DataBaseType = ConfigurationManager.AppSettings["DAL"];

        public DataTable LinkList(string SqlStr)
        {
            string cateId = "";
            ArrayList paraNameArr = new ArrayList();
            ArrayList constDefaultvalueArr = new ArrayList();
            string[] strArray = SqlStr.Split(new char[] { '$' });
            string tempSqlStr = strArray[0];
            string pageSize = strArray[1];
            string selectDir = strArray[3];
            string[] strArray2 = strArray[4].Split(new char[] { ',' });
            paraNameArr = DcmsCommSqlProcess.GetParaNameArr(strArray[0], ref constDefaultvalueArr);
            if (paraNameArr.Count > 0)
            {
                for (int i = 0; i < paraNameArr.Count; i++)
                {
                    string str6 = paraNameArr[i].ToString().ToLower();
                    if (str6 != null)
                    {
                        if (!(str6 == "linkid"))
                        {
                            if (str6 == "linkcateid") goto Label_021A;
                        }
                        else if (strArray2[i].ToLower() == "con")
                            tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), constDefaultvalueArr[i].ToString());
                        else if (DcmsRequest.GetQueryInt("LinkId", 0) != 0)
                            tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), HttpContext.Current.Request.QueryString["LinkId"].ToString());
                        else
                            tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), constDefaultvalueArr[i].ToString());
                    }
                    continue;
                Label_021A:
                    if (strArray2[i].ToLower() == "con")
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(constDefaultvalueArr[i].ToString(), selectDir) + ")");
                    else if (DcmsRequest.GetQueryInt("LinkCateId", 0) != 0)
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(HttpContext.Current.Request.QueryString["LinkCateId"].ToString(), selectDir) + ")");
                    else if (DcmsRequest.ValidaQueryStr("LinkCateId"))
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(Utils.check_TxtSql(HttpContext.Current.Request.QueryString["LinkCateId"].ToString()), selectDir) + ")");
                    else
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(constDefaultvalueArr[i].ToString(), selectDir) + ")");
                }
            }
            else
                tempSqlStr = tempSqlStr.Replace("(" + cateId + ")", "(" + DcmsCommSqlProcess.GetCateListInfo(cateId, selectDir) + ")");
            return DcmsCommSqlProcess.ReturnTableData(tempSqlStr, pageSize, null);
        }

        public string LinkModuleSql(string ModelName, string FileList, string ParaName, string DefaultValue, string SelectDir, string ConditionStr, string NumStr, string IsPage, string OrderFileStr, string SortStr, string ParaTypeStr)
        {
            string[] strArray3;
            int num2;
            string str = "";
            string str2 = "";
            if (NumStr == "") NumStr = "1";
            if (OrderFileStr == "") OrderFileStr = "Link_Id";
            if (SortStr == "") SortStr = "desc";
            if (IsPage == "") IsPage = "false";
            if (ParaTypeStr == "") ParaTypeStr = "con";
            string[] strArray = ParaTypeStr.Split(new char[] { ',' });
            string str3 = ParaTypeStr;
            if (ParaName != "")
            {
                string[] strArray2 = ParaName.Split(new char[] { ',' });
                strArray3 = DefaultValue.Split(new char[] { '|' });
                string str4 = "";
                for (num2 = 0; num2 < strArray2.Length; num2++)
                {
                    string str7 = strArray2[num2].ToLower();
                    if (str7 != null)
                    {
                        if (!(str7 == "linkid"))
                        {
                            if (str7 == "linkcateid") goto Label_01AA;
                        }
                        else
                        {
                            if (strArray3.Length > num2)
                                str4 = strArray3[num2];
                            else
                                str4 = "1";
                            if (strArray.Length < num2) str3 = str3 + strArray[strArray.Length - 1];
                            str = str + "Link_Id=[LinkId#" + str4 + "] and ";
                        }
                    }
                    continue;
                Label_01AA:
                    if (strArray3.Length > num2)
                        str4 = strArray3[num2];
                    else
                        str4 = "1";
                    if (strArray.Length < num2) str3 = str3 + strArray[strArray.Length - 1];
                    str = str + "Link_CateId in [LinkCateId#" + str4 + "] and ";
                }
            }
            else
            {
                strArray3 = DefaultValue.Split(new char[] { ',' });
                string str5 = "";
                for (num2 = 0; num2 < strArray3.Length; num2++)
                {
                    str5 = str5 + strArray3[num2] + ",";
                }
                if (str5.Length > 0)
                    str5 = str5.Substring(0, str5.Length - 1);
                else
                    str5 = "0";
                str = str + "Link_CateId in(" + str5 + ") and ";
            }
            ConditionStr = ConditionStr.ToLower();
            if (ConditionStr.IndexOf("link_exflag1") >= 0) str = str + "Link_ExFlag1='1' and ";
            if (ConditionStr.IndexOf("link_pic") >= 0) str = str + "len(Link_Pic)>0 and ";
            if (ConditionStr.IndexOf("link_pic1") >= 0) str = str + "len(Link_Pic)=0 and ";
            if (str.Length > 4)
                str = str + "Link_State='1'";
            else
                str = "Link_State='1'";
            if (IsPage.ToLower() == "true")
            {
                str2 = "select " + FileList + " from Dcms_Link where " + str + " order by " + OrderFileStr + " " + SortStr;
                return (str2 + "$" + NumStr + "$" + ModelName + "$" + SelectDir + "$" + str3);
            }
            str2 = "select top " + NumStr + " " + FileList + " from Dcms_Link where " + str + " order by " + OrderFileStr + " " + SortStr;
            return (str2 + "$0$" + ModelName + "$" + SelectDir + "$" + str3);
        }

        public string SpecialFieldResolve(string HtmlTempLate, string ModelName, DataRow Datadr)
        {
            return HtmlTempLate;
        }
    }
}

